﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Collections.Generic
{
    /// <summary>
    /// 针对<see cref="List{T}"/>进行扩展
    /// </summary>
    public static class CollectionExtensions
    {
        /// <summary>
        /// 判断数据源是否为空，或者长度为0
        /// </summary>
        public static bool IsNullOrEmpty<T>(this ICollection<T> source)
        {
            return source == null || source.Count <= 0;
        }

        /// <summary>
        /// 往列表中新增一项。若不存在该项，则添加，否则不再添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">源</param>
        /// <param name="item">表示添加项</param>
        public static void AddIfNotContains<T>(this ICollection<T> source, T item)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Contains(item))
            {
                return;
            }

            source.Add(item);
        }

        /// <summary>
        /// Adds items to the collection which are not already in the collection.
        /// </summary>
        /// <typeparam name="T">Type of the items in the collection</typeparam>
        /// <param name="source">The collection</param>
        /// <param name="items">Item to check and add</param>
        /// <returns>Returns the added items.</returns>
        public static IEnumerable<T> AddIfNotContains<T>(this ICollection<T> source, IEnumerable<T> items)
        {
            List<T> list = new List<T>();
            foreach (T item in items)
            {
                if (!source.Contains(item))
                {
                    source.Add(item);
                    list.Add(item);
                }
            }

            return list;
        }

        /// <summary>
        /// Adds an item to the collection if it's not already in the collection based on the given predicate.
        /// </summary>
        /// <typeparam name="T">Type of the items in the collection</typeparam>
        /// <param name="source">The collection</param>
        /// <param name="predicate">The condition to decide if the item is already in the collection</param>
        /// <param name="itemFactory">A factory that returns the item</param>
        /// <returns>Returns True if added, returns False if not.</returns>
        public static bool AddIfNotContains<T>(this ICollection<T> source, Func<T, bool> predicate, Func<T> itemFactory)
        {
            if (source.Any(predicate))
            {
                return false;
            }

            source.Add(itemFactory());
            return true;
        }

        /// <summary>
        /// Removes all items from the collection those satisfy the given predicate.
        /// </summary>
        /// <typeparam name="T">Type of the items in the collection</typeparam>
        /// <param name="source">The collection</param>
        /// <param name="predicate">The condition to remove the items</param>
        /// <returns>List of removed items</returns>
        public static IList<T> RemoveAll<T>(this ICollection<T> source, Func<T, bool> predicate)
        {
            List<T> list = source.Where(predicate).ToList();
            foreach (T item in list)
            {
                source.Remove(item);
            }

            return list;
        }

        /// <summary>
        /// Removes all items from the collection.
        /// </summary>
        /// <typeparam name="T">Type of the items in the collection</typeparam>
        /// <param name="source">The collection</param>
        /// <param name="items">Items to be removed from the list</param>
        public static void RemoveAll<T>(this ICollection<T> source, IEnumerable<T> items)
        {
            foreach (T item in items)
            {
                source.Remove(item);
            }
        }

        /// <summary>
        /// 若新值不为空，则添加；否则，不添加
        /// </summary>
        /// <typeparam name="T">新值的数据类型</typeparam>
        /// <param name="source">The collection</param>
        /// <param name="item">Item to be add to the list</param>
        public static void AddIfNotNull<T>(this ICollection<T> source, T item)
        {
            if (source != null && item != null)
            {
                source.Add(item);
            }
        }
    }
}
